
#include"calserver.hpp"
#include"log.hpp"
#include<iostream>
#include<stdlib.h>
#include<memory>
using namespace Server;
using namespace std;

static void Usage(string proc)
{
    cout<<"\nUsage:\n\t"<<proc<<" local_port\n\n"<<endl;
}
//req是一个已经处理的完整的对象
bool cal(const Request& req,Response& rep)
//根据req填充rep 
{
switch(req._op)
{
    case '+':
    rep._result=req._x+req._y;
    break;
    case '-':
        rep._result=req._x-req._y;
    break;    
    case '*':
        rep._result=req._x*req._y;
    break;    
    case '/':
    {
        if(req._y==0) rep._exitcode=DIV_ZERO;
        else
        rep._result=req._x/req._y;
    }   
    break;    
    case '%':
    {
        if(req._y==0) rep._exitcode=MOD_ZERO;
        else
        rep._result=req._x%req._y;
    }
    break;
    default:
    rep._exitcode=OP_ERR;
    break;
}
return true;

}

int main(int argc,char* argv[])
{
if(argc!=2)
{
    Usage(argv[0]);
    exit(USAGE_ERR);
}

uint16_t port=atoi(argv[1]);//将字符串转化为整数

unique_ptr<calserver> ts(new calserver(port));
ts->initserver();
ts->start(cal);


return 0;
}
